package cn.sinjet.communication.bluetooth;


import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.util.Log;
import cn.sinjet.communication.bluetooth.LeBluetoothService.BleServiceCallback;
import cn.sinjet.model.carassist.AppModel;

public class LeBluetooth {
//	private static final String tag = "BLE";
	private Context mContext = null;
//	private static LeBluetooth mLeBluetooth = null;
	private LeBluetoothService mLeBluetoothService = null;
	private BluetoothManager mBluetoothManager = null;
	private BluetoothAdapter mBluetoothAdapter = null;
	
	public LeBluetooth(Context context){
	     this.mContext = context;	
	}
	
	/*public synchronized static LeBluetooth getInstance(Context context){
		if(mLeBluetooth == null)
			mLeBluetooth = new LeBluetooth(context);
		return mLeBluetooth;
	}*/
	
	@SuppressLint("NewApi") public boolean initLeBluetooth(){
		if(mBluetoothManager == null){
			   mBluetoothManager = (BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE);
			}
			if(mBluetoothManager == null) return false;
			
			if(mBluetoothAdapter == null)
			mBluetoothAdapter = mBluetoothManager.getAdapter();
			if(mBluetoothAdapter == null) return false;
			
			if(!mBluetoothAdapter.isEnabled()){
				Log.i("BT", "BT not enable");
				 if(AppModel.getInstance().getSettings().bAutoOpenBT){
						Log.i("BT", "BT try to  enable");
				    mBluetoothAdapter.enable();
				 }
				 else return false;
			}
			
			if(mLeBluetoothService == null){
				Intent gattServiceIntent = new Intent(mContext,LeBluetoothService.class);
				boolean bRet = mContext.bindService(gattServiceIntent, mBleServiceConnection,
						Context.BIND_AUTO_CREATE);
				if(!bRet)
					return false;
			}
			return true;
	}
	
	public interface LeBluetoothCallback{
		void onBTStateChange(int state);
		void onReceived(byte[] message,int len);
	}
	LeBluetoothCallback mLeBluetoothCallback = null;
	public void setLeBluetoothCallback(LeBluetoothCallback leBluetoothCallback){
		mLeBluetoothCallback = leBluetoothCallback;
	}
	
	BleServiceCallback mBleServiceCallback = new BleServiceCallback(){
		@Override
		public void onBTStateChange(int state) {
			// TODO Auto-generated method stub
			if(mLeBluetoothCallback != null)
				mLeBluetoothCallback.onBTStateChange(state);
		}
	
		@Override
		public void onReceived(byte[] message,int len) {
			// TODO Auto-generated method stub
			if(mLeBluetoothCallback != null)
				mLeBluetoothCallback.onReceived(message,len);
		}
	  };
	  
	private final ServiceConnection mBleServiceConnection = new ServiceConnection() {
		@Override
		public void onServiceConnected(ComponentName componentName,
				IBinder service) {
			mLeBluetoothService = ((LeBluetoothService.LocalBinder) service).getService();
			mLeBluetoothService.setBleServiceCallback(mBleServiceCallback);
			BluetoothModel.getInstance().connectBT();
//            if(mLeBluetoothService != null)
//            	mLeBluetoothService.requestConnection();
		}

		@Override
		public void onServiceDisconnected(ComponentName componentName) {
			mLeBluetoothService = null;
		}
	};
	
	public void sendMessage(byte[] message){
		if(mLeBluetoothService != null)
			mLeBluetoothService.sendMessage(message);
	}
	
	public void setBleServiceCallback(BleServiceCallback callback){
		if(mLeBluetoothService != null)
			mLeBluetoothService.setBleServiceCallback(callback);
	}
	
	public void requestConnect(){
		if(mLeBluetoothService != null)
			mLeBluetoothService.requestConnection();
	}

	public void requestDisconnect() {
		// TODO Auto-generated method stub
		if(mLeBluetoothService != null)
			mLeBluetoothService.disconnectDevice();
	}

	public void clearBT() {
		// TODO Auto-generated method stub
		if (mLeBluetoothService != null && mLeBluetoothService.isBTConnected())
			mLeBluetoothService.disconnectDevice();

		if (mBleServiceConnection != null && mLeBluetoothService != null) {
			mContext.unbindService(mBleServiceConnection);
			mLeBluetoothService = null;
		}
	}

	
	public void tryToKeepConnected() {
		// TODO Auto-generated method stub
		Log.d("BLE", "tryToKeepConnected()");
		if(mLeBluetoothService == null){
			initLeBluetooth();
			return;
		}
		if(!mLeBluetoothService.isBTConnected() && !mLeBluetoothService.isInConnecttingPeriod())
			mLeBluetoothService.requestConnection();
	}
}
